package com.youmingxing.dialog;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DownloadManager;
import android.content.DialogInterface;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;

import com.youmingxing.R;
import com.youmingxing.data.GlobalDataManager;
import com.youmingxing.network.GlobalDownloadManager;
import com.youmingxing.view.DialogManager;

/**
 * @author evilyin(ChenZhixi)
 * @since 15/11/6
 */
public class UpdateDialog extends DialogFragment {

    public static final int forceUpdate = 1;
    public static final int normalUpdate = 2;
    public static final int notice=3;

    private int updateType;
    private String des;
    private String link;
    DialogInterface.OnClickListener listener;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            updateType = bundle.getInt("type");
            des = bundle.getString("des");
            link = bundle.getString("link");
        }
        initListener();
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        switch (updateType) {
            case forceUpdate:
                return createForceDialog();
            case normalUpdate:
            case notice:
                return createNormalDialog();
            default:
                return super.onCreateDialog(savedInstanceState);
        }
    }

    private Dialog createForceDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(R.string.update).setMessage(des).setCancelable(false).
        setPositiveButton(R.string.update_now, listener);
        return builder.create();
    }

    private Dialog createNormalDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(R.string.update).setMessage(des).setPositiveButton(R.string.update_now,
                listener).setNegativeButton(R.string.cancel, null);
        return builder.create();
    }

    private void initListener() {
        listener = new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                startDownload(link);
            }
        };
    }

    private void startDownload(String url) {
        DialogManager.showForceDialog(getActivity(),"正在下载更新");
        DownloadManager downloadManager = GlobalDownloadManager.getInstance(getActivity());
        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
        request.setTitle(getActivity().getResources().getString(R.string.app_name));
        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,
                "allstar" + System.currentTimeMillis() + ".apk");
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        GlobalDataManager.downloadId = downloadManager.enqueue(request);
        GlobalDataManager.save();
    }

}
